package com.sign2024.sign2024project.projectservice.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sign2024.sign2024core.common.CommonResult;
import com.sign2024.sign2024core.common.annotation.Log;
import com.sign2024.sign2024core.common.constants.CommonConstants;
import com.sign2024.sign2024core.common.exeception.CommonException;
import com.sign2024.sign2024core.common.utils.StringUtils;
import com.sign2024.sign2024core.entry.projectservice.PlanInventory;
import com.sign2024.sign2024core.entry.projecttask.ProjectTask;
import com.sign2024.sign2024project.aop.listener.PlanInventoryExcelListener;
import com.sign2024.sign2024project.projectservice.param.PlanInventoryParam;
import com.sign2024.sign2024project.projectservice.service.PlanInventoryService;
import com.sign2024.sign2024project.projecttask.service.ProjectTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author yujie
 * @data 2024/7/24 15:16
 */
//@Api(tags = "计划清单")
@Api(tags = "工程服务")
@RestController
@RequestMapping("/api/planInventory")
public class PlanInventoryController {

    @Resource
    private PlanInventoryService planInventoryService;

    @Resource
    private ProjectTaskService projectTaskService;


    /**
     * 计划清单excel导入
     *
     * @param file
     * @param taskId 项目任务id
     * @return
     * @throws IOException
     */
    @Log(title = "工程服务", businessType = "导入")
    @ApiOperation(value = "计划清单excel导入", notes = "计划清单excel导入")
    @Transactional
    @PostMapping("/import")
    public CommonResult<PlanInventory> importInfo(MultipartFile file, Integer taskId) {
        if (StringUtils.isNull(taskId)) {
            throw new CommonException(CommonConstants.ERROR, "任务标识为空");
        }

        // 删除该任务下的数据
        planInventoryService.deleteByTaskId(taskId);
        // 将 任务 id 进行赋值
        PlanInventoryExcelListener listener = new PlanInventoryExcelListener();
        listener.setTaskId(taskId);
        try {
            EasyExcel.read(file.getInputStream(), listener).head(PlanInventory.class).sheet().doReadSync();

            // 导入数据后， 将任务中的计划清单列修改为 已导入
            ProjectTask projectTask = new ProjectTask();
            projectTask.setId(taskId);
            projectTask.setPlanInventory("已导入");
            projectTaskService.updateTask(projectTask);
            return CommonResult.success();

        } catch (IOException e) {
            e.printStackTrace();
            throw new CommonException(CommonConstants.ERROR, "excel数据导入失败");
        }
    }

    @Log(title = "工程服务", businessType = "查询")
    @ApiOperation(value = "分页查询计划清单", notes = "分页查询计划清单")
    @PostMapping("/page")
    public CommonResult<PlanInventory> page(PlanInventoryParam param) {
        Page<PlanInventory> pageList = planInventoryService.pagePlanInventory(param);
        return CommonResult.success(pageList);
    }
}
